package iredigo

import (
	"gitee.com/tietang/terrace-go/v2/base"
	"gitee.com/tietang/terrace-go/v2/lib/gedis"
	"github.com/gomodule/redigo/redis"
	log "github.com/sirupsen/logrus"
	"github.com/tietang/props/v3/kvs"
	"time"
)

const (
	timeout = 10 * time.Second
)

var pool *redis.Pool

func RedigoPool() *redis.Pool {
	base.Check(pool)
	return pool
}

type RedigoStarter struct {
	base.BaseStarter
	pool *redis.Pool
	opts *gedis.Options
}

func (s *RedigoStarter) Setup(ctx base.StarterContext) {
	conf := ctx.Props()
	s.opts = &gedis.Options{}

	err := kvs.Unmarshal(conf, s.opts, "redis")
	if err != nil {
		log.Error(err)
	}
	s.pool = gedis.NewPool(s.opts)
	pool = s.pool
}

func (s *RedigoStarter) Stop(ctx base.StarterContext) {
	s.pool.Close()
}
